package com.lyshop.jinding.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.Typeface;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.util.AttributeSet;

import androidx.appcompat.widget.AppCompatRadioButton;

import com.lyshop.jinding.R;


/**
 * Created by Mr on 2018/10/17.
 */

public class MyRadioButton extends AppCompatRadioButton {
    private int mSmallLength;
    private int mBigLength;
    private float mBigSize;
    private int mBigColor;

    public MyRadioButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MyRadioButton);
        mSmallLength = array.getInt(R.styleable.MyRadioButton_smallLength, 0);
        mBigLength = array.getInt(R.styleable.MyRadioButton_bigLength, 0);
        mBigSize = array.getDimension(R.styleable.MyRadioButton_bigSize, 0);
        mBigColor = array.getColor(R.styleable.MyRadioButton_bigColor, Color.WHITE);
        array.recycle();
        setContent(getText());
    }

    public void setBigColor(int color){
        this.mBigColor = color;
        setContent(getText());
    }

    public void setBigLength(int bigLength){
        this.mBigLength = bigLength;
        setContent(getText());
    }

    public void setContent(CharSequence text) {
        SpannableStringBuilder span = new SpannableStringBuilder(text);

        if(mBigLength > 0){
            span.setSpan(new ForegroundColorSpan(mBigColor), 0, mBigLength,
                    Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            span.setSpan(new AbsoluteSizeSpan((int) mBigSize), 0, mBigLength,
                    Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            span.setSpan(new StyleSpan(Typeface.BOLD), 0, mBigLength,
                    Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        }else{
            span.setSpan(new ForegroundColorSpan(mBigColor), 0, text.length() - mSmallLength,
                    Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            span.setSpan(new AbsoluteSizeSpan((int) mBigSize), 0, text.length() - mSmallLength,
                    Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            span.setSpan(new StyleSpan(Typeface.BOLD), 0, text.length() - mSmallLength,
                    Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        }
        setText(span);
    }
}
